package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.domain.entity.AddressBook;
import com.example.mapper.AddressBookMapper;
import com.example.service.IAddressBookService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 地址管理 服务实现类
 * </p>
 *
 * @author xiaoduan
 * @since 2024-09-24
 */
@Service
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook> implements IAddressBookService {

    @Override
    public List<AddressBook> listByUserId(Long userId) {

        return baseMapper.selectByUserId(userId);
    }

    @Override
    public boolean updateDefault(Long id, Long userId) {
        List<AddressBook> list = list(new LambdaQueryWrapper<AddressBook>().eq(AddressBook::getUserId, userId ));
        for (AddressBook addressBook : list) {
            addressBook.setIsDefault(0);
        }
        saveOrUpdateBatch(list);
        AddressBook one = getOne(new LambdaQueryWrapper<AddressBook>().eq(AddressBook::getId, id ).eq(AddressBook::getUserId, userId ));
        one.setIsDefault(1);
        saveOrUpdate(one);
        return true;
    }

}
